<title>本地安装</title>
  
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>应用管理</cite></a>
    <a><cite>本地安装</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
      <div class="layui-card-header">应用管理</div>
        <div class="layui-card-body">
          <div class="layui-tab layui-tab-brief" lay-filter="J_tabLi">
            <ul class="layui-tab-title">
              <li lay-href="plugin/manage">已安装</li>
              <li class="layui-this" lay-href="plugin/local">本地安装</li>
            </ul>
            <div class="layui-tab-content">
            	<div class="layui-tab-item layui-show">
		          <div style="padding-bottom: 10px;padding-top: 10px;">
		            <blockquote class="layui-elem-quote">目前仅支持.zip压缩包<br>请上传压缩包，然后点立即安装开始安装流程<br>如果安装失败，可按照错误提示修复错误，然后选择重试</blockquote>
		            <button type="button" class="layui-btn" id="addon-upload"><i class="layui-icon layui-icon-upload"></i>上传文件</button>
		          </div>
		          <table id="J_table_plugin_list" lay-filter="J_table_plugin_list"></table>
            	</div>
            </div>
          </div>
          
        </div>
      </div>
      
    </div>
  </div>
</div>

<script type="text/html" id="J_table_tool">
    <button name="{{d.name}}" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="install">安装</button>
    <button name="{{d.name}}" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="remove">删除</button>
</script>

<script>
;!function(){
  var $ = layui.$
  ,upload = layui.upload
  ,table = layui.table;

  //执行渲染
table.render({
  elem: '#J_table_plugin_list'
  ,url: '{:url('/plugin/local')}'
  ,cols: [[
      {field:'name',title:'未安装插件',minWidth:200}
      ,{title:'操作',width:150,align:'center',toolbar:'#J_table_tool'}
  ]]
  ,height: 'auto'
  ,text: {
  	none: '暂无数据'
  }
});

  
  var action = {
  	remove: function(addonids){
	    var _this = $(this);
	    $.get('{:url('/plugin/remove')}',{addonids:addonids},function(res){
			var org_text = _this.text();
  	  	  	_this.text(org_text.replace(/(\u4e2d\u002e\u002e\u002e)$/, ''));
  	  	  	_this.removeProp('disabled').removeClass('layui-btn-disabled');
	        layer.msg(res.msg,{},function(){
	          	if(res.code===0){
	          		window.location.reload();
	          	}
			});
	    },'json');
  	},
  	install: function(addonids){
	    var _this = $(this);
	    $.get('{:url('/plugin/install')}',{addonids:addonids},function(res){
	        if(res.code===0){
	        	setTimeout('check("'+addonids+'")',2000);
	        }else{
				var org_text = _this.text();
		  	  	_this.text(org_text.replace(/(\u4e2d\u002e\u002e\u002e)$/, ''));
		  	  	_this.removeProp('disabled').removeClass('layui-btn-disabled');
	            layer.msg(res.msg);
	        }
	    },'json');
  	}
  };
  
	table.on("tool(J_table_plugin_list)", function(obj){
		event.preventDefault();
		var plugin = $(this).prop('name');
		var textnode = document.createTextNode('\u4e2d\u002e\u002e\u002e');
  		this.appendChild(textnode);
  		$(this).prop('disabled',true).addClass('layui-btn-disabled');
		action[obj.event] ? action[obj.event].call(this,plugin) : '';
	});
	
  upload.render({
      elem: '#addon-upload'
      ,url: '{:url('/plugin/upload')}'
      ,accept: 'file' //普通文件
      ,exts: 'zip' //只允许上传压缩文件
      ,done: function(res){
      	layer.msg(res.msg,{time:2000},function(){
      		if(res.code===0){
      			window.location.reload();
      		}
      	});
      }
    });
    
}();
function check(addonids){
	    layui.$.get('{:url('/plugin/check')}',{addonids:addonids},function(res){
	        if(res.code===0){
	          layer.msg('安装完毕',{
				  time: 2000
				}, function(){
				  window.location.reload();
				});
	        }else if(res.code===200){
	          setTimeout('check("'+addonids+'")',2000);
	        }else{
	          layer.msg(res.msg);
	        }
	    },'json');
}
</script>